package com.i2works.smartluolong.web.controller.index;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.utils.plugin.Pager;
import com.i2works.smartluolong.web.entity.Implement.ImplementDO;
import com.i2works.smartluolong.web.entity.Implement.ImplementFilesDO;
import com.i2works.smartluolong.web.entity.Implement.ImplementFilesQuery;
import com.i2works.smartluolong.web.entity.Implement.ImplementQuery;
import com.i2works.smartluolong.web.entity.common.SearchParam;
import com.i2works.smartluolong.web.entity.org.OrgDO;
import com.i2works.smartluolong.web.entity.org.OrgQuery;
import com.i2works.smartluolong.web.service.implement.IImplementFilesService;
import com.i2works.smartluolong.web.service.implement.IImplementService;
import com.i2works.smartluolong.web.service.org.IOrgService;
import com.i2works.smartluolong.web.utils.BaseModule;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 搜索控制器
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/8/26 22:04
 */

@Controller
@RequestMapping("/itemList")
public class ItemListController extends BaseModule {


    
    @Autowired
    private  IImplementService  implementService;
    
    @Autowired
    private IImplementFilesService implementFilesService;
    
    @Autowired
    private IOrgService orgService;


    /**
     * 个人事项
     */
    @GetMapping(value = {"/person","/person-{page:[0-9]*}-{itemZT}-{itemDX}-{itemHD}-{deptCode}"})
    public String personList(@ModelAttribute SearchParam searchParam, Model model) {
        try {
            if (searchParam.getPage() == null || searchParam.getPage() == 0) {
                searchParam.setPage(1);
            }
                Integer pageNumber = searchParam.getPage();
                List<ImplementDO> itemsList = Lists.newArrayList();
                ImplementQuery itemsQuery = new ImplementQuery();
                itemsQuery.setStatus("Y");
                itemsQuery.setForUser("01");
                itemsQuery.setPersonZt(StringUtils.isEmpty(searchParam.getItemZT())?null:searchParam.getItemZT());
                itemsQuery.setPersonTdrq(StringUtils.isEmpty(searchParam.getItemDX())?null:searchParam.getItemDX());
                itemsQuery.setPersonRssj(StringUtils.isEmpty(searchParam.getItemHD())?null:searchParam.getItemHD());
                itemsQuery.setDeptCode( StringUtils.isEmpty(searchParam.getDeptCode())?null:searchParam.getDeptCode());
                Integer queryCount = implementService.queryCount(itemsQuery);
                if (queryCount > 0) {
                    itemsQuery.setOffset(itemsQuery.getLimit() * (pageNumber - 1));
                    itemsList = implementService.queryList(itemsQuery);
                }
                //事项列表
                model.addAttribute("itemsList", itemsList);
                //事项面向对象
                model.addAttribute("itemType", "person");
                //主题
                model.addAttribute("itemZT", StringUtils.isEmpty(searchParam.getItemZT())?null:searchParam.getItemZT());
                //对象
                model.addAttribute("itemDX", StringUtils.isEmpty(searchParam.getItemDX())?null:searchParam.getItemDX());
                //活动
                model.addAttribute("itemHD", StringUtils.isEmpty(searchParam.getItemHD())?null:searchParam.getItemHD());
                //部门编码
                model.addAttribute("deptCode", StringUtils.isEmpty(searchParam.getDeptCode())?null:searchParam.getDeptCode());
                //部门列表
                model.addAttribute("deptList", getDeptList());
                //分页实体
                model.addAttribute("pager", new Pager(pageNumber, queryCount,10));

        } catch (Exception e) {
            model.addAttribute("error", MsgEnum.SEARCH_ERROR.getMsg());
            logger.error("ItemController.personList个人事项列表异常：" + e.getMessage());
        }
        return "itemList/list";
    }
    


    /**
     * 法人事项
     */
    @GetMapping(value = {"/legal","/legal-{page:[0-9]*}-{itemZT}-{itemDX}-{itemHD}-{deptCode}"})
    public String legalList(@ModelAttribute SearchParam searchParam, Model model) {
        try {
            if (searchParam.getPage() == null || searchParam.getPage() == 0) {
                searchParam.setPage(1);
            }
                Integer pageNumber = searchParam.getPage();
                List<ImplementDO> itemsList = Lists.newArrayList();
                ImplementQuery itemsQuery = new ImplementQuery();
                itemsQuery.setStatus("Y");
                itemsQuery.setForUser("02");
                itemsQuery.setEnterpriseZt(StringUtils.isEmpty(searchParam.getItemZT())?null:searchParam.getItemZT());
                itemsQuery.setEnterpriseTddx( StringUtils.isEmpty(searchParam.getItemDX())?null:searchParam.getItemDX());
                itemsQuery.setEnterpriseJyhd(StringUtils.isEmpty(searchParam.getItemHD())?null:searchParam.getItemHD());
                itemsQuery.setDeptCode(StringUtils.isEmpty(searchParam.getDeptCode())?null:searchParam.getDeptCode());
                Integer queryCount = implementService.queryCount(itemsQuery);
                if (queryCount > 0) {
                    itemsQuery.setOffset(itemsQuery.getLimit() * (pageNumber - 1));
                    itemsList = implementService.queryList(itemsQuery);
                }
                //事项列表
                model.addAttribute("itemsList", itemsList);
                //事项
                model.addAttribute("itemType", "legal");
                //主题
                model.addAttribute("itemZT", StringUtils.isEmpty(searchParam.getItemZT())?null:searchParam.getItemZT());
                //对象
                model.addAttribute("itemDX", StringUtils.isEmpty(searchParam.getItemDX())?null:searchParam.getItemDX());
                //活动
                model.addAttribute("itemHD", StringUtils.isEmpty(searchParam.getItemHD())?null:searchParam.getItemHD());
                //部门编码
                model.addAttribute("deptCode", StringUtils.isEmpty(searchParam.getDeptCode())?null:searchParam.getDeptCode());
                //部门列表
                model.addAttribute("deptList", getDeptList());
                //分页实体
                model.addAttribute("pager", new Pager(pageNumber, queryCount,10));

        } catch (Exception e) {
            model.addAttribute("error", MsgEnum.SEARCH_ERROR.getMsg());
            logger.error("ItemController.legalList法人事项列表异常：" + e.getMessage());
        }
        return "itemList/list";
    }
    
    /**
     * 部门事项
     */
    @GetMapping(value = {"/dept/{deptCode:[0-9]*}","/dept/{deptCode:[0-9]*}-{page:[0-9]*}-{serviceType}"})
    public String deptList(@ModelAttribute SearchParam searchParam, Model model) {
        try {
            if (searchParam.getPage() == null || searchParam.getPage() == 0) {
                searchParam.setPage(1);
            }
	            OrgQuery query=new  OrgQuery();
	            query.setOrgCode(searchParam.getDeptCode());
	            query.setStatus(1);
                OrgDO org= orgService.query(query);
                //事项列表
                model.addAttribute("deptName", org.getOrgName());
                Integer pageNumber = searchParam.getPage();
                List<ImplementDO> itemsList = Lists.newArrayList();
                ImplementQuery itemsQuery = new ImplementQuery();
                itemsQuery.setStatus("Y");
                itemsQuery.setLimit(5L);
                itemsQuery.setDeptCode(StringUtils.isEmpty(searchParam.getDeptCode())?null:searchParam.getDeptCode());
                itemsQuery.setServiceType(StringUtils.isEmpty(searchParam.getServiceType())?null:searchParam.getServiceType());
                Integer queryCount = implementService.queryCount(itemsQuery);
                if (queryCount > 0) {
                    itemsQuery.setOffset(itemsQuery.getLimit() * (pageNumber - 1));
                    itemsList = implementService.queryList(itemsQuery);
                }
                Set<String> set=new HashSet<String>();
                for (ImplementDO item : itemsList) {
                	set.add(item.getServiceType());
				}
                

                //事项列表
                model.addAttribute("itemsList", itemsList);
                //类型
                model.addAttribute("itemType", "dept");
                //事项列表
                model.addAttribute("serviceTypeMap", getServiceTypeMap(searchParam.getDeptCode()));
                //部门编码
                model.addAttribute("deptCode", searchParam.getDeptCode());
                //服务类型
                model.addAttribute("serviceType",searchParam.getServiceType());
                //分页实体
                model.addAttribute("pager", new Pager(pageNumber, queryCount,5));

        } catch (Exception e) {
            model.addAttribute("error", MsgEnum.SEARCH_ERROR.getMsg());
            logger.error("ItemController.deptList部门事项列表异常：" + e.getMessage());
        }
        return "itemList/dept";
    }
    
    /**
     * 办事指南
     */
    @GetMapping(value = {"/detail/{uuid}"})
    public String detail(@PathVariable("uuid") String unid, Model model) {
    	 ImplementQuery itemsQuery = new ImplementQuery();
         itemsQuery.setUnid(unid);
         try {
         ImplementDO item=	implementService.query(itemsQuery);
         model.addAttribute("item", item);
         ImplementFilesQuery filesQuery = new ImplementFilesQuery();
         filesQuery.setServiceId(item.getUnid());
         List<ImplementFilesDO> filesList = implementFilesService.queryList(filesQuery);
         model.addAttribute("filesList", filesList);
		} catch (ServiceException e) {
	     logger.error("ItemListController.detail办事指南异常：" + e.getMessage());
		}
		return "itemList/detail";
    }
    
    
    private Map<String, String> getServiceTypeMap(String  deptCode){
    	Map<String, String> map =new HashMap<String, String> ();
    	try {
    	Integer count=0;
        ImplementQuery query = new ImplementQuery();
        query.setStatus("Y");
        query.setDeptCode(deptCode);
    	query.setServiceType("XK");
		count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("XK", "行政许可");
    	}
     	query.setServiceType("JC");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("JC", "行政检查");
    	}
    	query.setServiceType("QZ");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("QZ", "行政强制");
    	}
    	query.setServiceType("ZC");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("ZC", "行政征收");
    	}
     	query.setServiceType("GF");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("GF", "行政给付");
    	}
     	query.setServiceType("CJ");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("CJ", "行政裁决");
    	}
    	query.setServiceType("QR");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("QR", "行政确认");
    	}
    	query.setServiceType("JL");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("JL", "行政奖励");
    	}
    	query.setServiceType("GG");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("GG", "公共服务");
    	}
    	query.setServiceType("QT");
        count = implementService.queryCount(query);
    	if(count!=0) {
    		map.put("QT", "其他权力");
    	}
    	} catch (ServiceException e) {
    		 logger.error("ItemListController.getServiceType异常：" + e.getMessage());
		}
    	return map;
    }
    
    
    public List<OrgDO> getDeptList(){
    	List<OrgDO> deptList=Lists.newArrayList();
    	OrgQuery query=new OrgQuery();
    	query.setStatus(1);
    	query.setLimit(500L);
    	try {
			deptList=orgService.queryList(query);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
    	return deptList;
    	
    }
    
    
}
